﻿//https://leetcode.cn/problems/palindrome-partitioning/submissions/613406112/?envType=study-plan-v2&envId=top-100-liked

class Solution {
    vector<vector<int>> f;
    vector<vector<string>> ret;
    vector<string> path;
    int n;
public:
    vector<vector<string>> partition(string s)
    {
        n = s.size();
        f.assign(n, vector<int>(n, true));
        for (int i = n - 1; i >= 0; i--)
        {
            for (int j = i + 1; j < n; j++)
            {
                f[i][j] = (s[i] == s[j]) && f[i + 1][j - 1];
            }
        }

        dfs(s, 0);
        return ret;
    }

    void dfs(string& s, int i)
    {
        if (i == n)
        {
            ret.push_back(path);
            return;
        }

        for (int j = i; j < n; j++)
        {
            if (f[i][j])
            {
                path.push_back(s.substr(i, j - i + 1));
                dfs(s, j + 1);
                path.pop_back();
            }
        }
    }
};